package com.offcn.test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class CASTest2 {
    public static void main(String[] args) throws InterruptedException {
        long l1 = System.currentTimeMillis();
        AtomicInteger ai = new AtomicInteger(0);
        CountDownLatch cd = new CountDownLatch(1000);
        for (int i=0;i<1000;i++){
            new Thread(()->{
                for (int x=0;x<100;x++){
                    //ai.get()获得当前的值
                    while (!ai.compareAndSet(ai.get(), ai.get()+1));
                }
                cd.countDown();
            }).start();
        }
        cd.await();
        long l2 = System.currentTimeMillis();
        System.out.println(ai.get());
        System.out.println("运行时间:"+(l2-l1));
    }
}
